// pages/TradingAreaCenter/shopCircleDetail.js
const controller = require("../controllers/Controller").controller
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: "",
    detail: {},
    pageNumber: 1,
    pageNumber2: 1,
    cardList: [],
    bagList: [],
    allShopList: [],
    lastPage: false,
    lastPage2: false,
    isShowDetail:false,
    shopId:'',
  },
  //获取商圈详情
  getShopCircleDetail() {
    let value = {}
    value.shopGroupId = this.data.id
    controller.getShopCircleDetail(value).then(res => {
      this.setData({
        detail: res.data
      })
    })
  },
  //获取店铺列表
  getShopList() {
    controller.myShop().then(res => {
      this.setData({
        allShopList: res.data
      })
    })
  },
  //选择店铺
  bindPickerChange(e) {
    let id = this.data.allShopList[e.detail.value].id
    let isHad = this.data.detail.shopList.some(item => id == item.id)
    if (isHad) {
      wx.showToast({
        icon: "none",
        title: '商圈内已存在该店铺',
      })
    } else {
      this.addShop(id)
    }
  },
  //添加店铺
  addShop(id) {
    let value = {}
    value.agentId = wx.getStorageSync('agentId')
    value.shopGroupId = this.data.id
    value.shopIds = id
    controller.addShop(value).then(res => {
      wx.showToast({
        icon: "none",
        title: '操作成功'
      })
      this.getShopCircleDetail()
    })
  },

  //获取一卡通和店铺爆款列表
  getActivityPage(type) {
    let value = {}
    value.shopGroupId = this.data.id
    value.type = type
    if (type === 0) {
      value.pageNumber = this.data.pageNumber
    } else {
      value.pageNumber = this.data.pageNumber2
    }
    controller.getActivityPage(value).then(res => {
      res.data.list.forEach(item => {
        switch (item.timeState) {
          case 0:
            item.stateName = "未开始";
            item.stateColor = "grey";
            break;
          case 1:
            item.stateName = "进行中";
            item.stateColor = "green";
            break;
          case 2:
            item.stateName = "结束";
            item.stateColor = "red";
            break;
        }
      })

      if (type === 0) {
        this.setData({
          cardList: res.data.list,
          lastPage: res.data.lastPage
        })
      } else {
        this.setData({
          bagList: res.data.list,
          lastPage2: res.data.lastPage
        })
      }
    })
  },

  getMore() {
    if (this.data.lastPage == false || this.data.lastPage2 == false) {
      let value = {};
      let value2 = {}
      value.shopGroupId = this.data.id
      value2.shopGroupId = this.data.id
      value.type = 0
      value2.type = 2
      value.pageNumber = this.data.pageNumber + 1
      value2.pageNumber = this.data.pageNumber2 + 1
      this.setData({
        pageNumber: value.pageNumber,
        pageNumber2: value2.pageNumber
      })
      controller.getActivityPage(value).then(res => {
        res.data.list.forEach(item => {
          switch (item.timeState) {
            case 0:
              item.stateName = "未开始";
              item.stateColor = "grey";
              break;
            case 1:
              item.stateName = "进行中";
              item.stateColor = "green";
              break;
            case 2:
              item.stateName = "结束";
              item.stateColor = "red";
              break;
          }
        })
        let list = this.data.cardList
        let list2 = res.data.list
        let list3 = list.concat(list2)
        this.setData({
          cardList: list3,
          lastPage: res.data.lastPage
        })
      })

      controller.getActivityPage(value2).then(res => {
        res.data.list.forEach(item => {
          switch (item.timeState) {
            case 0:
              item.stateName = "未开始";
              item.stateColor = "grey";
              break;
            case 1:
              item.stateName = "进行中";
              item.stateColor = "green";
              break;
            case 2:
              item.stateName = "结束";
              item.stateColor = "red";
              break;
          }
        })
        let list = this.data.bagList
        let list2 = res.data.list
        let list3 = list.concat(list2)
        this.setData({
          bagList: list3,
          lastPage2: res.data.lastPage
        })
      })
    }
  },
  // 踢出商圈中店铺
  delShop(e) {
    let value = {
      shopId: e.target.dataset.id,
      agentId: this.data.detail.agentId,
      shopGroupId: this.data.id,
      sessionId: wx.getStorageSync('token')
    }
    controller.groupKick(value).then(res => {
      this.getShopCircleDetail()
    })
  },

  showDetail(e) {
    this.setData({
      isShowDetail: true,
      shopId: e.target.dataset.id,
    })
  },
  closeDetail(){
    this.setData({
      isShowDetail: false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id
    })
    this.getShopList()
    this.getShopCircleDetail()
    this.getActivityPage(0)
    this.getActivityPage(2)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getMore()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})